package cn.blue.service.impl;

import cn.blue.constant.RedisConstants;
import cn.blue.domain.House;
import cn.blue.exception.GlobalExceptionCodeEnum;
import cn.blue.mapper.HouseMapper;
import cn.blue.result.JSONResult;
import cn.blue.service.IHouseService;
import cn.blue.utils.AssertUtil;
import cn.blue.utils.DistanceUtil;
import cn.blue.utils.Point;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huihui
 * @since 2022-12-09
 */
@Service

public class HouseServiceImpl extends ServiceImpl<HouseMapper, House> implements IHouseService {

    @Autowired
    private RedisTemplate<Object,Object>redisTemplate;

    @Override
    @CacheEvict(cacheNames = RedisConstants.REDIS_HOUSE_SOURCE_CACHENAMES,key = RedisConstants.REDIS_HOUSE_SOURCE_KEY)
    public JSONResult online(Long houseId) {
        House house = selectById(houseId);
        AssertUtil.isNotNull(house, GlobalExceptionCodeEnum.HOUSE_IS_NOT_EXIST);
        house.setState(1);
        updateById(house);
        return JSONResult.success(GlobalExceptionCodeEnum.HOUSE_ONLINE_SUCCESS);
    }

    @Cacheable(cacheNames = RedisConstants.REDIS_HOUSE_SOURCE_CACHENAMES,key = RedisConstants.REDIS_HOUSE_SOURCE_KEY)
    @Override
    public List<House> showHouse() {
        EntityWrapper<House> wrapper = new EntityWrapper<>();
        wrapper.eq("state",1);
        List<House> houses = selectList(wrapper);

       return houses;
    }


    @Override
    @CacheEvict(cacheNames = RedisConstants.REDIS_HOUSE_SOURCE_CACHENAMES,key = RedisConstants.REDIS_HOUSE_SOURCE_KEY)
    public JSONResult undercarriage(Long houseId) {
        House house = selectById(houseId);
        AssertUtil.isNotNull(house, GlobalExceptionCodeEnum.HOUSE_IS_NOT_EXIST);
        house.setState(0);
        updateById(house);
        return JSONResult.success(GlobalExceptionCodeEnum.HOUSE_LOWER_SHELF);
    }

    /**
     * 寻找周围10公里的房源
     * @return
     */
    @Override
    public List<House> position() {
        EntityWrapper<House> wrapper = new EntityWrapper<>();
        wrapper.eq("state",1);
        //模拟位置测试
        Point customPoint = new Point(104.05740358713781, 30.697356042874134);
        AssertUtil.isNotNull(customPoint,GlobalExceptionCodeEnum.POINT_IS_NOT_EXIST);
        List<House> list = new ArrayList<>();
        List<House> houses = selectList(wrapper);
        for (House h : houses) {
            Point point = DistanceUtil.getPoint(h.getHouseAddress());
            AssertUtil.isNotNull(point, GlobalExceptionCodeEnum.POINT_IS_NOT_EXIST);
            double distance = DistanceUtil.getDistance(customPoint, point);
            if (distance < 10000) {
                list.add(h);
            }
        }
        return list;

    }

    @CacheEvict(cacheNames = RedisConstants.REDIS_HOUSE_SOURCE_CACHENAMES,key = RedisConstants.REDIS_HOUSE_SOURCE_KEY)
    @Override
    public boolean insert(House entity) {
        return super.insert(entity);
    }


    @CacheEvict(cacheNames = RedisConstants.REDIS_HOUSE_SOURCE_CACHENAMES,key = RedisConstants.REDIS_HOUSE_SOURCE_KEY)
    @Override
    public boolean updateById(House entity) {
        return super.updateById(entity);
    }


    @CacheEvict(cacheNames = RedisConstants.REDIS_HOUSE_SOURCE_CACHENAMES ,key = RedisConstants.REDIS_HOUSE_SOURCE_KEY)
    @Override
    public boolean deleteById(Serializable id) {
        return super.deleteById(id);
    }
}
